/**
 * creation: 7-mrt-2008
 */
package org.jfonia.connect;

import java.util.ArrayList;

/**
 * An implementation of Callable which reacts to call() by calling zero or more
 * other Callables. This class can be used to implement constructs like a
 * procedure or code-block. It can also be used to inform value listeners about
 * a value change. Etc.
 * 
 * <p>
 * The order of callables (listeners) is preserved. Currently implemented using
 * an ArrayList, which allows duplicates!
 * 
 * @author wijnand.schepens@gmail.com
 * 
 */
public class BasicNode implements Node
{
	protected ArrayList<Callable> callTargets = new ArrayList<Callable>();
	

	// Override only when necessary - it is preferable to override callThis()
	public boolean call(Object param)
	{
		return this.callThis(param) && this.callTargets(param);
	}

	// extending classes should override this method: 
	public boolean callThis(Object param)
	{
		return true;
	}

	public void addCallTarget(Callable callable)
	{
		callTargets.add(callable);
	}
	
	public void removeCallTarget(Callable callable)
	{
		callTargets.remove(callable);
	}

	public void clearCallTargets()
	{
		callTargets.clear();
	}

	/**
	 * Calls all callables (listeners) passing 'this' as parameter.
	 * @returns true if any of the callables returns true
	 */
	public boolean callTargets(Object param)
	{
		boolean result = false;
		for (Callable c: callTargets)
			result |= c.call(this);
		return true;
	}
	
}
